using System;
using System.ComponentModel;
using Jink.Core.Converters.Annotations;
using XenoGears.Assertions;

namespace Jink.Core.Converters.Default
{
    [Converter]
    internal static class StringConverters
    {
        [Converter(Weight = 0.01)]
        public static String Serialize<T>(T obj)
        {
            var converter = TypeDescriptor.GetConverter(typeof(T));
            converter.CanConvertTo(typeof(String)).AssertTrue();
            return converter.ConvertToInvariantString(obj.AssertThat(_ => obj != null));
        }

        [Converter(Weight = 0.01)]
        public static T Deserialize<T>(String s)
        {
            var converter = TypeDescriptor.GetConverter(typeof(T));
            converter.CanConvertFrom(typeof(String)).AssertTrue();
            return (T)converter.ConvertFromInvariantString(s.AssertNotNull());
        }
    }
}